// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten. You can (and have to!) safely modify it by hand.
 */
package org.tinaelle.server.service;

import java.util.Collection;
import java.util.List;

import org.tinaelle.ServiceLocator;
import org.tinaelle.common.dto.AdTreeDTO;
import org.tinaelle.common.dto.AdTreeNodeDTO;
import org.tinaelle.server.domain.AdTree;
import org.tinaelle.server.domain.AdTreeDao;
import org.tinaelle.server.domain.AdTreeNode;
import org.tinaelle.server.domain.AdTreeNodeDao;
import org.tinaelle.utils.BootstrapUtils;

/**
 * @see org.tinaelle.service.AdTreeService
 */
public class AdTreeServiceImpl extends org.tinaelle.server.service.AdTreeServiceBase {

	/**
     * 
     */
	private static final long	serialVersionUID	= 1L;

	@Override
	protected AdTreeDTO handleGetTreeByName(final String name) throws Exception {
		return ( AdTreeDTO ) getAdTreeDao().findByName( AdTreeDao.TRANSFORM_ADTREEDTO, name );
	}

	@Override
	protected AdTreeNodeDTO handleGetTreeNodeByName(final String name, final AdTreeDTO adTreeDTO) throws Exception {
		final AdTree adTree = getAdTreeDao().load( adTreeDTO.getAdTreeId() );
		return ( AdTreeNodeDTO ) getAdTreeNodeDao().findByName( AdTreeNodeDao.TRANSFORM_ADTREENODEDTO, name, adTree );
	}

	@Override
	protected AdTreeNodeDTO[] handleGetTreeNodes(final Integer adTreeId) throws Exception {
		final List<AdTreeNode> rootNodes = getAdTreeNodeDao().getRootNode( adTreeId );

		return getAdTreeNodeDao().toAdTreeNodeDTOArray( rootNodes );
	}

	@Override
	protected AdTreeNodeDTO[] handleGetNodes(final Integer adTreeNodeId) throws Exception {
		final AdTreeNode node = ( AdTreeNode ) getAdTreeNodeDao().load( AdTreeNodeDao.TRANSFORM_NONE, adTreeNodeId );
		final Collection<AdTreeNode> childrens = node.getChildrens();
		return getAdTreeNodeDao().toAdTreeNodeDTOArray( childrens );
	}

	@Override
	protected void handleFillInitialValues() throws Exception {
		return;
		/*
		 * AdTreeDTO menuTree = getTreeByName( "System Menu" ); if ( menuTree == null ) { menuTree = new AdTreeDTO(); }
		 * menuTree.setName( "System Menu" ); menuTree.setType(
		 * ServiceLocator.instance().getAdParameterService().getParameterValueByName( "TREE", "Default Tree"
		 * ).getAdParameterValueId() ); menuTree = save( menuTree );
		 * 
		 * AdTreeNodeDTO treeNodeMenu1 = getTreeNodeByTitle( "Menu 1", menuTree ); if ( treeNodeMenu1 == null ) {
		 * treeNodeMenu1 = new AdTreeNodeDTO(); treeNodeMenu1.setAdTreeId( menuTree.getAdTreeId() );
		 * treeNodeMenu1.setTitle( "Menu 1" );
		 * 
		 * treeNodeMenu1 = save( treeNodeMenu1 ); } AdTreeNodeDTO treeNodeMenu11 = getTreeNodeByTitle( "Menu 1.1",
		 * menuTree ); if ( treeNodeMenu11 == null ) { treeNodeMenu11 = new AdTreeNodeDTO(); treeNodeMenu11.setAdTreeId(
		 * menuTree.getAdTreeId() ); treeNodeMenu11.setTitle( "Menu 1.1" ); treeNodeMenu11.setParentId(
		 * treeNodeMenu1.getAdTreeNodeId() );
		 * 
		 * treeNodeMenu11 = save( treeNodeMenu11 ); } AdTreeNodeDTO treeNodeMenu2 = getTreeNodeByTitle( "Menu 2",
		 * menuTree ); if ( treeNodeMenu2 == null ) { treeNodeMenu2 = new AdTreeNodeDTO(); treeNodeMenu2.setAdTreeId(
		 * menuTree.getAdTreeId() ); treeNodeMenu2.setTitle( "Menu 2" );
		 * 
		 * save( treeNodeMenu2 ); }
		 */
	}

	@Override
	protected void handleFillInitialValuesForTree() throws Exception {
		final ServiceLocator serviceLocator = ServiceLocator.instance();
		final AdSystemService adSystemSrv = serviceLocator.getAdSystemService();

		// File Format :
		// Name, Description, Type : 0, 1, 2
		String[][] treeContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdTree" );
		for ( int i = 1; i < treeContent.length; i++ ) {
			final String[] treeLine = treeContent[ i ];

			if ( treeLine.length > 1 ) {
				AdTreeDTO adTreeDTO = getTreeByName( treeLine[ 0 ].trim() );
				if ( adTreeDTO == null ) {
					adTreeDTO = new AdTreeDTO();
				}

				adTreeDTO.setName( treeLine[ 0 ].trim() );
				adTreeDTO.setDescription( treeLine[ 1 ].trim() );
				adTreeDTO.setType( adSystemSrv.getReferenceListByValue( "TREE_TYPE", treeLine[ 2 ].trim() )
				        .getAdReferenceListId() );
				adTreeDTO = save( adTreeDTO );
			}
		}
	}

	@Override
	protected void handleFillInitialValuesForTreeNode() throws Exception {
		final ServiceLocator serviceLocator = ServiceLocator.instance();
		final AdSystemService adSystemSrv = serviceLocator.getAdSystemService();
		final AdWindowService adWindowSrv = serviceLocator.getAdWindowService();
		// File Format :
		// Name, Ref Tree Name, Icon : 0, 1, 2
		// Thumbnail, Tooltip, SeqNo : 3, 4, 5
		// RecordId, Ref Parent Name, Ref Screen Name : 6, 7, 8
		// Ref Table Name : 9
		String[][] treeNodeContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdTreeNode" );
		for ( int i = 1; i < treeNodeContent.length; i++ ) {
			final String[] treeNodeLine = treeNodeContent[ i ];

			if ( treeNodeLine.length > 1 ) {
				AdTreeDTO adTreeDTO = getTreeByName( treeNodeLine[ 1 ].trim() );
				AdTreeNodeDTO adTreeNodeDTO = getTreeNodeByName( treeNodeLine[ 0 ].trim(), adTreeDTO );
				if ( adTreeNodeDTO == null ) {
					adTreeNodeDTO = new AdTreeNodeDTO();
				}
				adTreeNodeDTO.setAdTreeId( adTreeDTO.getAdTreeId() );
				adTreeNodeDTO.setName( treeNodeLine[ 0 ].trim() );
				adTreeNodeDTO.setIconCls( treeNodeLine[ 2 ].trim() );
				adTreeNodeDTO.setThumbnail( treeNodeLine[ 3 ].trim() );
				adTreeNodeDTO.setTooltip( treeNodeLine[ 4 ].trim() );
				if ( ( treeNodeLine[ 5 ] != null ) && ( treeNodeLine[ 5 ].trim().length() > 0 ) ) {
					adTreeNodeDTO.setSeqNo( Integer.parseInt( treeNodeLine[ 5 ].trim() ) );
				}
				if ( treeNodeLine.length > 6 ) {
					if ( ( treeNodeLine[ 6 ] != null ) && ( treeNodeLine[ 6 ].trim().length() > 0 ) ) {
						adTreeNodeDTO.setRecordId( Integer.parseInt( treeNodeLine[ 6 ].trim() ) );
					}
					if ( ( treeNodeLine[ 7 ] != null ) && ( treeNodeLine[ 7 ].trim().length() > 0 ) ) {
						adTreeNodeDTO.setParentId( getTreeNodeByName( treeNodeLine[ 7 ], adTreeDTO ).getAdTreeNodeId() );
					}
					if ( ( treeNodeLine[ 8 ] != null ) && ( treeNodeLine[ 8 ].trim().length() > 0 ) ) {
						adTreeNodeDTO.setAdScreenId( adWindowSrv.getScreenByName( treeNodeLine[ 8 ].trim() )
						        .getAdScreenId() );
					}

					if ( treeNodeLine.length > 9 ) {
						if ( ( treeNodeLine[ 9 ] != null ) && ( treeNodeLine[ 9 ].trim().length() > 0 ) ) {
							adTreeNodeDTO.setAdTableId( adSystemSrv.getTableByName( treeNodeLine[ 9 ].trim() )
							        .getAdTableId() );
						}
					}
				}

				adTreeNodeDTO = save( adTreeNodeDTO );
			}
		}
	}

}